package com.yixiang.np.mapper;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;


@Service
public abstract class BaseService<T, PK extends Serializable> {

	@Autowired
	protected Mapper<T> mapper;

	public T getEntityByPrimaryKey(PK id) {
		return mapper.selectByPrimaryKey(id);
	}

	public int save(T entity) {
		return mapper.insert(entity);
	}

	public int delete(PK entity) {
		return mapper.deleteByPrimaryKey(entity);
	}

	public int update(T entity) {
		return mapper.updateByPrimaryKey(entity);
	}

	public int updatebySelective(T entity) {
		return mapper.updateByPrimaryKeySelective(entity);
	}

	public int saveSelective(T entity) {
		return mapper.insertSelective(entity);
	}
	
	/**
	 * 单表分页查询
	 *
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<T> selectPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.select(null);
	}

	/**
	 * 根据查询条件分页查询
	 * @param pageNum
	 * @param pageSize
	 * @param example
	 * @return
	 */
	public List<T> selectPageByExample(int pageNum, int pageSize, Example example) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectByExample(example);
	}

}